using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using CodRevit.Models;
using CodRevit.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace CodRevit.ViewModels
{
    public partial class CozeBotViewModel : ObservableObject
    {
        private readonly ICozeApiService _apiService;

        [ObservableProperty]
        private bool _isInitialView = true;

        [ObservableProperty]
        private bool _isChatView = false;

        [ObservableProperty]
        private string _userInput;

        [ObservableProperty]
        private ObservableCollection<ChatMessage> _chatMessages = new ObservableCollection<ChatMessage>();

        public CozeBotViewModel(ICozeApiService apiService)
        {
            _apiService = apiService;
        }

        [RelayCommand]
        private async Task SendMessage()
        {
            if (string.IsNullOrWhiteSpace(UserInput))
                return;

            if (IsInitialView)
            {
                IsInitialView = false;
                IsChatView = true;
            }

            var userMessage = new ChatMessage
            {
                Role = "user",
                Content = UserInput
            };

            ChatMessages.Add(userMessage);
            var currentInput = UserInput;
            UserInput = string.Empty;

            var response = await _apiService.SendMessageAsync(currentInput);
            ChatMessages.Add(response);
        }

        [RelayCommand]
        private async Task AskQuestion(string question)
        {
            if (IsInitialView)
            {
                IsInitialView = false;
                IsChatView = true;
            }

            UserInput = question;
            await SendMessage();
        }
    }
}